﻿Imports System.IO
Public Class HelpEditor
#Region "Dimension and Declarations"
    Public HelpFile As String
    Private rename As Boolean
    Private prevname As String
    Private prevtypef As String
    Private prevdata As String
    Private prevlines As String
#End Region
    Public Function GetFileContents(ByVal FullPath As String, _
        Optional ByRef ErrInfo As String = "") As String
        Dim strContents As String
        Dim objReader As StreamReader
        Try
            objReader = New StreamReader(FullPath)
            strContents = objReader.ReadToEnd()
            Return strContents
            objReader.Close()
            objReader.Dispose()
        Catch Ex As Exception
            Return ""
            ErrInfo = Ex.Message
            MsgBox(ErrInfo, MsgBoxStyle.Critical, "ERROR!")
        End Try
    End Function
    Public Function SaveTextToFile(ByVal strData As String, _
     ByVal FullPath As String, _
       Optional ByVal ErrInfo As String = "") As Boolean
        Dim bAns As Boolean = False
        Dim trynum As Short = 1
        Dim objReader As StreamWriter
        While bAns = False
            Try
                objReader = New StreamWriter(FullPath)
                objReader.Write(strData)
                objReader.Close()
                bAns = True
            Catch Ex As Exception
                ErrInfo = Ex.Message
                If trynum = 1000 Then
                    MsgBox(ErrInfo, MsgBoxStyle.Critical, "ERROR!")
                    bAns = True
                End If
            End Try
        End While
        Return bAns
    End Function
    Private Sub LoadHelpEditor(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tempvar As Integer, tempvar2 As Integer
        Dim ItemNumber As Integer = 1
        Dim ContinueCycle As Boolean = True
        HelpFilesList.Items.Clear()
        HelpFile = GetFileContents(Debugger.DirHelpFiles)
        While ContinueCycle = True
            If InStr(HelpFile, "#" + LTrim(Str(ItemNumber)) + "(") Then
                tempvar = InStr(HelpFile, "HELPF#" + LTrim(Str(ItemNumber)) + "(")
                If tempvar > 0 Then
                    For tempvar2 = InStr(HelpFile, "HELPF#" + LTrim(Str(ItemNumber)) + "(") To Len(HelpFile) Step 1
                        If Mid(HelpFile, tempvar2, 1) = ")" Then
                            HelpFilesList.Items.Add(LTrim(Str(ItemNumber)) + ". " + Mid(HelpFile, tempvar + 7 + Len(LTrim(RTrim(ItemNumber))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemNumber))))))
                            Exit For
                        End If
                    Next
                End If
                ItemNumber += 1
            Else
                ContinueCycle = False
            End If
        End While
        HelpFilesList.SelectedIndex = 0
    End Sub
    Private Sub ReplaceTypeReference(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HelpFilesList.SelectedIndexChanged
        LoadHelpLoc(HelpFilesList.SelectedIndex)
    End Sub
    Private Sub LoadHelpLoc(ByVal ItemNumber As Integer)
        ProperUsage.Text = "" : PresetData.Text = "" : Data.Text = ""
        Dim tempvar As Integer, tempvar2 As Integer
        ItemNumber += 1
        'Standard type reference
        tempvar = InStr(HelpFile, "TYPEF#" + LTrim(Str(ItemNumber)) + "(")
        If tempvar > 0 Then
            For tempvar2 = InStr(HelpFile, "TYPEF#" + LTrim(Str(ItemNumber)) + "(") To Len(HelpFile) Step 1
                If Mid(HelpFile, tempvar2, 1) = ")" Then
                    ProperUsage.Text += Mid(HelpFile, tempvar + 7 + Len(LTrim(RTrim(ItemNumber))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemNumber)))))
                    prevtypef = ProperUsage.Text
                    Exit For
                End If
            Next
        End If
        'standard data reference
        tempvar = InStr(HelpFile, "DATAF#" + LTrim(Str(ItemNumber)) + "(")
        If tempvar > 0 Then
            For tempvar2 = InStr(HelpFile, "DATAF#" + LTrim(Str(ItemNumber)) + "(") To Len(HelpFile) Step 1
                If Mid(HelpFile, tempvar2, 1) = ")" Then
                    TabControl1.SelectedTab = RegularTab
                    PresetTab.Enabled = False : RegularTab.Enabled = True
                    Data.Text += Mid(HelpFile, tempvar + 7 + Len(LTrim(RTrim(ItemNumber))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemNumber)))))
                    prevdata = Data.Text
                    Exit For
                End If
            Next
        End If
        'data reference for preset
        Dim lines() As String = PresetData.Lines
        ReDim Preserve lines(38) 'max of 39, starts w/ 0
        tempvar = InStr(HelpFile, "LIN00#" + LTrim(Str(ItemNumber)) + "(")
        If tempvar > 0 Then
            TabControl1.SelectedTab = PresetTab
            RegularTab.Enabled = False : PresetTab.Enabled = True
            ProperUsage.Text = "" 'prevents "help" from showing up on preset helps
            Dim TheRoundUp As Integer = 0, TheRoundText As String = "", Start As Integer = 0, LastFound As Integer = 0, StartFound As Boolean = False
            For TheRoundUp = 0 To 38 Step 1
                TheRoundText = LTrim(Str(TheRoundUp))
                If Len(TheRoundText) = 1 Then TheRoundText = "0" + TheRoundText
                tempvar = InStr(HelpFile, "LIN" + TheRoundText + "#" + LTrim(Str(ItemNumber)) + "(")
                If StartFound = False Then
                    Start = tempvar
                    StartFound = True
                End If
                If tempvar > 0 Then
                    For tempvar2 = InStr(HelpFile, "LIN" + TheRoundText + "#" + LTrim(Str(ItemNumber)) + "(") To Len(HelpFile) Step 1
                        If Mid(HelpFile, tempvar2, 1) = ")" Then
                            lines(TheRoundUp) = Mid(HelpFile, tempvar + 7 + Len(LTrim(RTrim(ItemNumber))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemNumber)))))
                            LastFound = tempvar2 + 2
                            Exit For
                        End If
                    Next
                End If
            Next
            PresetData.Lines = lines
            prevlines = Mid(HelpFile, Start, LastFound - Start)
        End If
    End Sub
    Private Sub AddBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBtn.Click
        NameHelp.Visible = True
        TypePanel.Visible = True
        TheName.Focus()
    End Sub
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Itemnumber As Integer = HelpFilesList.SelectedIndex
        Dim tempvar As Integer, tempvar2 As Integer
        Itemnumber += 1
        NameHelp.Visible = True
        'Type reference for preset
        tempvar = InStr(HelpFile, "HELPF#" + LTrim(Str(Itemnumber)) + "(")
        If tempvar > 0 Then
            For tempvar2 = InStr(HelpFile, "HELPF#" + LTrim(Str(Itemnumber)) + "(") To Len(HelpFile) Step 1
                If Mid(HelpFile, tempvar2, 1) = ")" Then
                    TheName.Text = Mid(HelpFile, tempvar + 7 + Len(LTrim(RTrim(Itemnumber))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(Itemnumber)))))
                    prevname = TheName.Text
                    Exit For
                End If
            Next
        End If
        rename = True
        TheName.Focus()
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TheName.Text = ""
        NameHelp.Visible = False
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If rename = True Then
            rename = False
            HelpFilesList.Items.Item(HelpFilesList.SelectedIndex) = Replace(HelpFilesList.Items.Item(HelpFilesList.SelectedIndex), prevname, TheName.Text)
            HelpFile = Replace(HelpFile, "(" + prevname + ")", "(" + TheName.Text + ")")
            NameHelp.Visible = False
        Else
            HelpFilesList.Items.Add(LTrim(Str(HelpFilesList.Items.Count + 1)) + ". " + TheName.Text)
            NameHelp.Visible = False
            TypePanel.Visible = False
            If StandardBtn.Checked Then
                HelpFile += Chr(13) + Chr(13) + "HELPF#" + LTrim(Str(HelpFilesList.Items.Count)) + "(" + TheName.Text + ")"
                HelpFile += Chr(13) + "TYPEF#" + LTrim(Str(HelpFilesList.Items.Count)) + "(BLANK)"
                HelpFile += Chr(13) + "DATAF#" + LTrim(Str(HelpFilesList.Items.Count)) + "(BLANK)"
                HelpFile += Chr(13) + "HELPN#" + LTrim(Str(HelpFilesList.Items.Count)) + "(" + LTrim(Str(HelpFilesList.Items.Count)) + ")"
            ElseIf PresetBtn.Checked Then
                HelpFile += Chr(13) + Chr(13) + "HELPF#" + LTrim(Str(HelpFilesList.Items.Count)) + "(" + TheName.Text + ")"
                HelpFile += Chr(13) + "TYPEF#" + LTrim(Str(HelpFilesList.Items.Count)) + "(BLANK)"
                HelpFile += Chr(13) + "LIN00#" + LTrim(Str(HelpFilesList.Items.Count)) + "(BLANK)"
                HelpFile += Chr(13) + "HELPN#" + LTrim(Str(HelpFilesList.Items.Count)) + "(" + LTrim(Str(HelpFilesList.Items.Count)) + ")"
            End If
        End If
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If RegularTab.Enabled = True Then
            HelpFile = Replace(HelpFile, "TYPEF#" + LTrim(Str(HelpFilesList.SelectedIndex + 1)) + "(" + prevtypef + ")", "TYPEF#" + LTrim(Str(HelpFilesList.SelectedIndex + 1)) + "(" + ProperUsage.Text + ")")
            HelpFile = Replace(HelpFile, "DATAF#" + LTrim(Str(HelpFilesList.SelectedIndex + 1)) + "(" + prevdata + ")", "DATAF#" + LTrim(Str(HelpFilesList.SelectedIndex + 1)) + "(" + Data.Text + ")")
            PresetData.Text = HelpFile
        ElseIf PresetTab.Enabled = True Then '
            Dim TheRoundUp As Integer = 0, TheRoundText As String = "", SaveText As String = "", TheText() As String = PresetData.Text.Split(Environment.NewLine)
            For TheRoundUp = LBound(TheText) To UBound(TheText)
                TheRoundText = LTrim(Str(TheRoundUp))
                If Len(TheRoundText) = 1 Then TheRoundText = "0" + TheRoundText
                SaveText += "LIN" + TheRoundText + "#" + LTrim(Str(HelpFilesList.SelectedIndex + 1)) + "(" + Trim(TheText(TheRoundUp)) + ")" + Environment.NewLine
            Next
            HelpFile = Replace(HelpFile, prevlines, SaveText)
            SaveTextToFile(HelpFile, Debugger.DirHelpFiles)
        End If
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ProperUsage.Text = "" : PresetData.Text = "" : Data.Text = ""
        Dim Itemnumber As String = LTrim(Str(HelpFilesList.SelectedIndex + 1))
        Dim tempvar As Integer, tempvar2 As Integer, ContinueCycle As Boolean = True
        PresetTab.Enabled = True
        tempvar = InStr(HelpFile, "HELPF#" + Itemnumber + "(")
        'REMOVE THE ITEM
        If tempvar > 0 Then
            tempvar2 = InStr(HelpFile, "HELPN#" + Itemnumber + "(")
            If tempvar2 > 0 Then
                HelpFile = Replace(HelpFile, Mid(HelpFile, tempvar, tempvar2 - tempvar + 11 + Len(Itemnumber) * 2), "")
            Else
                MessageBox.Show("Error locating selected index.", "ERROR 133 : Error locating selected index", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            MessageBox.Show("Error locating selected index.", "ERROR 134 : Error locating selected index", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        'MOVE LOWER ITEMS DOWN VNUMS
        Dim ItemNumberInteger As Integer = Val(Itemnumber) + 1
        While ContinueCycle = True
            If InStr(HelpFile, "#" + LTrim(Str(ItemNumberInteger)) + "(") Then
                HelpFile = Replace(HelpFile, "#" + LTrim(Str(ItemNumberInteger)) + "(" + LTrim(Str(ItemNumberInteger)) + ")", "#" + LTrim(Str(ItemNumberInteger - 1)) + "(" + LTrim(Str(ItemNumberInteger - 1)) + ")")
                HelpFile = Replace(HelpFile, "#" + LTrim(Str(ItemNumberInteger)) + "(", "#" + LTrim(Str(ItemNumberInteger - 1)) + "(")
                ItemNumberInteger += 1
            Else
                ContinueCycle = False
            End If
        End While
        'REPOPULATE LIST
        HelpFilesList.Items.Clear()
        ItemNumberInteger = 1
        ContinueCycle = True
        While ContinueCycle = True
            If InStr(HelpFile, "#" + LTrim(Str(ItemNumberInteger)) + "(") Then
                tempvar = InStr(HelpFile, "HELPF#" + LTrim(Str(ItemNumberInteger)) + "(")
                If tempvar > 0 Then
                    For tempvar2 = InStr(HelpFile, "HELPF#" + LTrim(Str(ItemNumberInteger)) + "(") To Len(HelpFile) Step 1
                        If Mid(HelpFile, tempvar2, 1) = ")" Then
                            HelpFilesList.Items.Add(LTrim(Str(ItemNumberInteger)) + ". " + Mid(HelpFile, tempvar + 7 + Len(LTrim(RTrim(ItemNumberInteger))), tempvar2 - (tempvar + 7 + Len(LTrim(RTrim(ItemNumberInteger))))))
                            Exit For
                        End If
                    Next
                End If
                ItemNumberInteger += 1
            Else
                ContinueCycle = False
            End If
        End While
        HelpFilesList.SelectedIndex = 0
    End Sub
End Class